/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.gp.patient.measurement;

import be.ugent.medirem.logic.MeasurementRemote;
import be.ugent.medirem.persistence.entity.details.MeasurementDetails;
import be.ugent.medirem.persistence.entity.details.MeasurementTypeDetails;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Simon
 */
public class PatientMeasurementServlet extends HttpServlet {

    @EJB
    private MeasurementRemote measurementBean;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Long id = (Long) request.getSession().getAttribute("pid");
        if (id == null) {
            request.getRequestDispatcher("/patient").forward(request, response);
            return;
        }

//        System.out.println(request.getAttribute("type"));  // null?

        try {
            List<MeasurementTypeDetails> types = new ArrayList<MeasurementTypeDetails>();

            List<MeasurementDetails> list = measurementBean.getAllMeasurements(id);
            Collections.sort(list, new Comparator<MeasurementDetails>() {

                public int compare(MeasurementDetails o1, MeasurementDetails o2) {
                    return o2.getDateTaken().compareTo(o1.getDateTaken());
                }

            });
            for (MeasurementDetails m : list) {
                MeasurementTypeDetails type = measurementBean.getMeasurementType(m.getTypeId());
                int i = 0;
                while (i < types.size()) {
                    if (!types.get(i).getName().equals(type.getName()))
                        i++;
                    else
                        break;
                }
                if (i == types.size())
                    types.add(type);
            }
            request.setAttribute("types",types);
            request.setAttribute("measurements", list);
        } catch (Exception ex) {
            request.getSession().setAttribute("flash", "An error occured when trying to get measurements!");
        }

        request.getRequestDispatcher("/gp/patient/measurement/list.jsp").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
